Spring Data JPA 入门学习笔记

一、概述

1、Spring Data JPA概述
  • Spring Data JPA 是 Spring 基于 ORM 框架、JPA 规范的基础上封装的一套JPA应用框架,可使开发者用极简的代码即可实现对数据库的访问和操作。它提供了包括增删改查等在内的常用功能,且易于扩展!学习并使用 Spring Data JPA 可以极大提高开发效率!
  • Spring Data JPA 让我们解脱了DAO层的操作,基本上所有CRUD都可以依赖于它来实现,在实际的工作工程中,推荐使用Spring Data JPA + ORM(如:hibernate)完成操作,这样在切换不同的ORM框架时提供了极大的方便,同时也使数据库层操作更加简单,方便解耦
2、Spring Data JPA的特性
  • SpringData Jpa 极大简化了数据库访问层代码。 如何简化的呢? 使用了SpringDataJpa,我们的dao层中只需要写接口,就自动具有了增删改查、分页查询等方法。
3、Spring Data JPA 与 JPA和 hibernate 之间的关系
  • JPA是一套规范,内部是有接口和抽象类组成的。
  • hibernate是一套成熟的ORM框架,而且Hibernate实现了JPA规范,所以也可以称hibernate为JPA的一种实现方式,我们使用JPA的API编程,意味着站在更高的角度上看待问题(面向接口编程)。
  • Spring Data JPA是Spring提供的一套对JPA操作更加高级的封装,是在JPA规范下的专门用来进行数据持久化的解决方案。

二、Spring Date JPA 整合Spring案例

在Spring Data JPA中,对于定义符合规范的Dao层接口,我们只需要遵循以下几点就可以了:

  • 1.创建一个Dao层接口,并实现JpaRepository和JpaSpecificationExecutor
  • 2.提供相应的泛型
第一步:编写配置文件 bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

    <!--数据源连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="username" value="root"></property>
        <property name="password" value="lemon"></property>
        <property name="url" value="jdbc:mysql:///db_hibernate" ></property>
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    </bean>
    <!--创建 entityManagerFactory 对象,交给spring容器来管理-->
    <bean id="entityManagerFactoty" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!--配置的扫描的包(实体类所在的包) -->
        <property name="packagesToScan" value="cn.lemon.domain" />
        <!-- jpa的实现厂家 -->
        <property name="persistenceProvider">
            <bean class="org.hibernate.jpa.HibernatePersistenceProvider"/>
        </property>

        <!--jpa的供应商适配器 -->
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <!--配置是否自动创建数据库表 -->
                <property name="generateDdl" value="false" />
                <!--指定数据库类型 -->
                <property name="database" value="MYSQL" />
                <!--数据库方言:支持的特有语法 -->
                <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
                <!--是否显示sql -->
                <property name="showSql" value="true" />
            </bean>
        </property>

        <!--jpa的方言 :高级的特性 -->
        <property name="jpaDialect" >
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
        </property>
    </bean>

    <!--事务-->
    <!--4.配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactoty"></property>
    </bean>

    <!-- 4.txAdvice-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="insert*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    <!-- 5.aop-->
    <aop:config proxy-target-class="true">
        <aop:pointcut id="pointcut" expression="execution(* cn.lemon.service.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
    </aop:config>

    <!--整合spring和jpa
        base-package:搜索dao所在的包,产生代理对象
    -->
    <jpa:repositories base-package="cn.lemon.dao" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactoty"></jpa:repositories>
</beans>
第二步:创建实体类 Customer.java、Order.java
package cn.lemon.domain;

import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "customer_")
public class Customer {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_")
    private Integer id;
    @Column(name = "CUST_NAME_")
    private String custName;
    @Column(name = "TELEPHONE_")
    private String telephone;
    @Column(name = "CREATE_DATE_")
    private Date createDate;
    @Column(name = "SEND_DATE_")
    private Date sendDate;
    @Column(name = "type_")
    private String type;
    @OneToMany(mappedBy = "customer")
    private Set<Order> orders = new HashSet<>();

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getCustName() {
        return custName;
    }

    public void setCustName(String custName) {
        this.custName = custName;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public Date getSendDate() {
        return sendDate;
    }

    public void setSendDate(Date sendDate) {
        this.sendDate = sendDate;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Set<Order> getOrders() {
        return orders;
    }

    public void setOrders(Set<Order> orders) {
        this.orders = orders;
    }
}
package cn.lemon.domain;

import javax.persistence.*;

@Entity
@Table(name = "order_")
public class Order {
    @Id
    @Column(name = "id_")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @Column(name = "PRODUCT_NAME_")
    private String productName;
    @Column(name = "PRICE_")
    private Double price;
    @Column(name = "NUM_")
    private Integer num;
    @ManyToOne
    @JoinColumn(name = "customer_id_")
    private Customer customer;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public Integer getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }
}
第三步:编写数据访问层dao,其中的接口要继承JpaRepository、JpaSpecificationExecutor
package cn.lemon.dao;

import cn.lemon.domain.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

public interface CustomerDao extends JpaRepository<Customer, Integer>, JpaSpecificationExecutor<Customer> {

}
package cn.lemon.dao;

import cn.lemon.domain.Order;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

public interface OrderDao extends JpaRepository<Order, Integer>, JpaSpecificationExecutor<Order> {

}
第四步:编写测试类
package cn.lemon.dao;

import cn.lemon.domain.Customer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/beans.xml")
public class CustomerDaoTest {
    @Autowired
    private CustomerDao customerDao;

    @Test
    public void testAdd() {
        Customer c = new Customer();
        c.setCustName("李三");
        c.setType("大客户");

        customerDao.save(c);
    }
}

三、Spring Data JPA的内部原理

  • Spring Data JPA是spring提供的一款对于数据访问层(Dao层)的框架,使用Spring Data JPA,只需要按照框架的规范提供dao接口,不需要实现类就可以完成数据库的增删改查、分页查询等方法的定义,极大的简化了我们的开发过程
  • 在使用Spring Data JPA时,一般实现JpaRepositoryJpaSpecificationExecutor接口,这样就可以使用这些接口中定义的方法,但是这些方法都只是一些声明,没有具体的实现方式,那么在 Spring Data JPA中它又是怎么实现的呢?
    在这里插入图片描述

在这里插入图片描述

四、Spring Data JPA的查询方式

在继承JpaRepository,和JpaRepository接口后,我们就可以使用接口中定义的方法进行查询,继承JpaRepository后的方法列表:
在这里插入图片描述
继承JpaSpecificationExecutor的方法列表:
在这里插入图片描述

1、使用JPQL(Java Persistence Query Language)的方式查询

修改 CustomerDao.java 接口

package cn.lemon.dao;

import cn.lemon.domain.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface CustomerDao extends JpaRepository<Customer, Integer>, JpaSpecificationExecutor<Customer> {
    //    @Query("select * from customer_ where cust_name_ like '%s%' and type_ = '大客户'")
    @Query("from Customer where custName like ?1 and type = ?2")
    public List<Customer> findAllCustomer(String custName, String type);
}

测试类

package cn.lemon.dao;

import cn.lemon.domain.Customer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/beans.xml")
public class JpqlDaoTest {
    @Autowired
    private CustomerDao customerDao;

    @Test
    public void testFindAllCustomer() {
        List<Customer> list = customerDao.findAllCustomer("%a%", "大客户");
        for (Customer customer : list) {
            System.out.println(customer.getCustName() + " " + customer.getType());
        }
    }
}
2、使用SQL语句查询

修改 CustomerDao.java 接口

package cn.lemon.dao;

import cn.lemon.domain.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

public interface CustomerDao extends JpaRepository<Customer, Integer>, JpaSpecificationExecutor<Customer> {

    /**
     * @Query:代表是是进行查询,声明此方法是用来更新操作
     * @Modifying:当前执行的是一个更新操作
     */
    @Query("update Customer set type = ?1 where type = ?2")
    @Modifying
    public void updateType(String type1, String type2);
}

测试类

package cn.lemon.dao;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/beans.xml")
public class JpqlDaoTest {
    @Autowired
    private CustomerDao customerDao;

    /**
     * rollback = false:测试时因为自动回滚,false表示不会滚
     */
    @Test
    @Transactional
    @Rollback(false)
    public void testUpdateType() {
        customerDao.updateType("超大客户", "大客户");
    }
}
3、方法命名规则查询
  • 顾名思义,方法命名规则查询就是根据方法的名字,就能创建查询。只需要按照Spring Data JPA提供的方法命名规则定义方法的名称,就可以完成查询工作。Spring Data JPA在程序执行的时候会根据方法名称进行解析,并自动生成查询语句进行查询
  • 按照Spring Data JPA 定义的规则,查询方法以findBy开头,涉及条件查询时,条件的属性用条件关键字连接,要注意的是:条件属性首字母需大写。框架在进行方法名解析时,会先把方法名多余的前缀截取掉,然后对剩下部分进行解析。
  • 具体的关键字,使用方法和生产成SQL如下表所示:

在这里插入图片描述
修改 CustomerDao.java 接口

package cn.lemon.dao;

import cn.lemon.domain.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

import java.util.List;

public interface CustomerDao extends JpaRepository<Customer, Integer>, JpaSpecificationExecutor<Customer> {
    /**
     * 方法命名规则查询
     * 前缀(findBy)+ 属性(CustName)+ 查询方式(Like)+ 连接符(And)+ 属性(Type)+ 方式(Equals)......
     */
    public List<Customer> findByCustNameLikeAndTypeEquals(String custName, String type);
}

测试类

package cn.lemon.dao;

import cn.lemon.domain.Customer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/beans.xml")
public class JpqlDaoTest {
    @Autowired
    private CustomerDao customerDao;

    @Test
    public void testFind2() {
        List<Customer> list = customerDao.findByCustNameLikeAndTypeEquals("%a%", "超大客户");
        for (Customer customer : list) {
            System.out.println(customer.getCustName() + " " + customer.getType());
        }
    }
}
4、分页查询

在测试类中添加分页查询

package cn.lemon.dao;

import cn.lemon.domain.Customer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/beans.xml")
public class JpqlDaoTest {
    @Autowired
    private CustomerDao customerDao;

    /**
     * 分页查询
     */
    @Test
    public void testFindPage() {
        //参数1:页码
        //参数2:每页行数
        Pageable pageable = PageRequest.of(0, 5);
        Page<Customer> page = customerDao.findAll(pageable);
        System.out.println("数据=" + page.getContent());
        System.out.println("总页数=" + page.getTotalPages());
        System.out.println("总行数=" + page.getTotalElements());
    }
}

五、Specifications动态查询

  • Specifications 原意:规范
  • 有时我们在查询某个实体的时候,给定的条件是不固定的,这时就需要动态构建相应的查询语句,在Spring Data JPA中可以通过JpaSpecificationExecutor接口查询。相比JPQL,其优势是类型安全,更加的面向对象。
package cn.lemon.dao;

import cn.lemon.domain.Customer;
import cn.lemon.domain.Order;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/beans.xml")
public class SpecificationQueryTest {
    @Autowired
    private CustomerDao customerDao;

    @Autowired
    private OrderDao orderDao;

    /**
     * specification查询等同于criteria查询,在匿名内部类中返回criteriaBuilder构建的条件
     * 具体criteria查询,参考qbc查询
     * root:Root接口,代表查询的根对象,可以通过root 获取实体类中的属性
     * query: 代表顶层的查询对象,用来自定义查询
     * cb:用来构建查询,此对象里有很多查询方法
     */
    @Test
    public void testSpecification1() {
        List<Customer> list = customerDao.findAll((root, query, builder) -> {
            Predicate p1 = builder.like(root.get("custName"), "%a%");
            Predicate p2 = builder.like(root.get("type"), "超大客户");
            return builder.and(p1, p2);
        });
        for (Customer customer : list) {
            System.out.println(customer.getCustName() + " " + customer.getType());
        }
    }

    /**
     * specification分页查询
     */
    @Test
    public void testSpecification2() {
        Pageable pageable = PageRequest.of(0,2);
        Page<Customer> page = customerDao.findAll((root, query, builder) -> {
            List<Predicate> list = new ArrayList<>();

//            //动态查询
//            if (true) { //条件成立
//                list.add(builder.like(root.get("custName"), "%a%"));
//            }
//            if (true) { //条件成立
//                list.add(builder.like(root.get("type"), "超大客户"));
//            }
//            Predicate[] predicates = (Predicate[]) list.toArray();
//            return builder.and(predicates);

            Predicate p1 = builder.like(root.get("custName"), "%a%");
            Predicate p2 = builder.like(root.get("type"), "超大客户");
            return builder.and(p1, p2);
        }, pageable);

        System.out.println("数据=" + page.getContent());
        System.out.println("总页数=" + page.getTotalPages());
        System.out.println("总行数=" + page.getTotalElements());
    }

    /**
     * specification分页查询
     */
    @Test
    public void testSpecification3() {
        List<Order> list = orderDao.findAll((root, query, builder) -> {
            Join<Order, Customer> join = root.join("customer", JoinType.INNER);
            return builder.equal(join.get("custName"), "张三三");
        });
        for (Order order : list) {
            System.out.println(order.getProductName());
        }
    }
}